package cn.q3c.parse.stock2.commons;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.xBaseJ.DBF;
import org.xBaseJ.xBaseJException;

import cn.q3c.parse.stock2.object.StockInfo;
import cn.q3c.parse.stock2.object.StockTradeLog;

public class StockUtils {

	private final static Logger logger = Logger.getLogger(StockUtils.class);

	/**
	 * 新浪格式数据文件，解析为对象 //1买盘2卖盘3中型盘
	 * 
	 * @param file
	 * @return
	 * @throws xBaseJException
	 * @throws IOException
	 * @throws ParseException
	 */
	public static List<StockTradeLog> toStockTradeLogList(int stockid,
			Date date, File file) throws IOException, ParseException {

		String dateStr = DateFormatUtils.format(date, "yyyy-MM-dd");

		List<StockTradeLog> retList = null;

		String encoding = "gbk";
		String s = FileUtils.readFileToString(file, encoding);
		if (StringUtils.isNotBlank(s)) {
			String tempS = StringUtils.removeStart(s, "[");
			tempS = StringUtils.removeStart(tempS, "]");

			String[] sArray = StringUtils.split(tempS, "\n");

			retList = new ArrayList<StockTradeLog>();

			for (int i = 1; i < sArray.length; i++) {
				if (StringUtils.isNotBlank(sArray[i])) {
					// logger.debug(sArray[i]);

					String[] tempArray = StringUtils.split(sArray[i], "	");
					String time = tempArray[0];
					float price = NumberUtils.toFloat(tempArray[1]);
					int count = NumberUtils.toInt(tempArray[3]);
					int priceCount = NumberUtils.toInt(tempArray[4]);
					int mmp = 0;

					if (tempArray.length >= 6) {
						if (StringUtils.equals(tempArray[5], "买盘")) {
							mmp = 1;
						} else if (StringUtils.equals(tempArray[5], "卖盘")) {
							mmp = 2;
						} else if (StringUtils.equals(tempArray[5], "中性盘")) {
							mmp = 3;
						}
					}

					StockTradeLog stockTradeLog = new StockTradeLog();
					stockTradeLog.setDirection(mmp);
					stockTradeLog.setPrice(price);
					stockTradeLog.setStockID(stockid);

					stockTradeLog.setVolume(count);
					stockTradeLog.setValue(priceCount);

					// logCJ.setCode(stockCode);
					// logCJ.setCount(count);
					// logCJ.setMmp(mmp);
					// logCJ.setPrice(price);
					// logCJ.setPriceCount(priceCount);
					// logCJ.setSctype(sctype);

					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						stockTradeLog.setTradingDate(sdf.parse(dateStr + " "
								+ time));
						stockTradeLog.setTradingTime(sdf.parse(dateStr + " "
								+ time));
						// logCJ.setCdate(sdf.parse(dateStr + " " + time));
					} catch (Exception e) {
						try {
							sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

							stockTradeLog.setTradingDate(sdf.parse(dateStr
									+ " " + time));
							stockTradeLog.setTradingTime(sdf.parse(dateStr
									+ " " + time));
							// logCJ.setCdate(sdf.parse(dateStr + " " + time));
						} catch (Exception ee) {

						}
					} finally {
						if (stockTradeLog.getTradingDate() == null) {
							logger.error("parse error:" + file.toString()
									+ ",stockid=" + stockid + ",date="
									+ dateStr + "," + sArray[i]);
							continue;
						}
					}

					retList.add(stockTradeLog);
				}
			}
		}

		return retList;
	}

	public static List<StockInfo> toStockInfoList(File file, int sctype) throws xBaseJException,
			IOException {
		String fileName = file.toString();
		List<StockInfo> retList = new ArrayList<StockInfo>();
		DBF dbf = new DBF(fileName, "gbk");

		if (sctype == StockInfo.SC_SHANGHAI) {

			for (int i = 0; i < dbf.getRecordCount(); i++) {
				dbf.read();
				String code = dbf.getField("S1").get();
				String name = dbf.getField("S2").get();
				StockInfo stockInfo = new StockInfo();
				stockInfo.setCode(code);
				stockInfo.setName(name);
				stockInfo.setExchange("sh");
				retList.add(stockInfo);
			}
		} else if (sctype == StockInfo.SC_SHENZHEN) {

			for (int i = 0; i < dbf.getRecordCount(); i++) {
				dbf.read();
				String code = dbf.getField(1).get();
				String name = dbf.getField(2).get();
				StockInfo stockInfo = new StockInfo();
				stockInfo.setCode(code);
				stockInfo.setName(name);
				stockInfo.setExchange("sz");
				retList.add(stockInfo);
			}

		}

		return retList;
	}
}